/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	SMeterInputConverter.java
 * Created：		2016年8月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.common;

import java.util.List;

import org.springframework.core.convert.converter.Converter;

import com.thor.commons.util.CollectionUtil;
import com.thor.palmmall.spi.common.SAttachment;

/**
 * @author wolv
 *
 */
public class SAttachmentConverter implements Converter<AAttachment, SAttachment> {

	public static SAttachmentConverter getInstance() {
		if (instance == null)
			instance = new SAttachmentConverter();
		return instance;
	}

	private static SAttachmentConverter instance;

	private SAttachmentConverter() {
	}

	@Override
	public SAttachment convert(AAttachment source) {
		if (source == null)
			return null;

		SAttachment target = new SAttachment();
		target.setFileType(getAttachmentType(source.getName()));
		target.setId(source.getId());
		target.setName(source.getName());
		target.setSize(source.getSize());
		target.setCaption(source.getCaption());
		return target;
	}

	private String getAttachmentCaption(String attachmentName) {
		if (attachmentName == null)
			return null;
		List<String> list = CollectionUtil.toList(attachmentName, '.');

		if (list.size() >= 1)
			return list.get(0);
		return attachmentName;

	}

	private String getAttachmentType(String attachmentName) {
		if (attachmentName == null)
			return null;
		List<String> list = CollectionUtil.toList(attachmentName, '.');

		if (list.size() >= 2)
			return list.get(1);
		return null;

	}
}
